(in-package :lilu-swank)

(defparameter *default-swank-port* 20000)
(defparameter *default-swank-interface* "localhost")

(defun create-swank-server (&key
                              (port *default-swank-port*)
                              (interface *default-swank-interface*)
                              (style swank:*communication-style*)
                              (log-event-p nil)
                              (dont-close-p nil)
                              (wait-p nil))
  (setf swank:*configure-emacs-indentation* nil
        swank::*enable-event-history* nil
        swank:*log-events* log-event-p)
  (swank:create-server :port port
                       :style style
                       :dont-close dont-close-p
                       :interface interface)
  (when wait-p
    (handler-case
        (bt:join-thread
         (find-if #'(lambda (n)
                      (alexandria:starts-with-subseq (format nil "Swank ~A" port) n))
                  (bt:all-threads)
                  :key #'bt:thread-name))
      (error (condition)
        (warn "~A" condition)))))

(defun destroy-swank-server (&key
                               (port *default-swank-port*))
  (swank:stop-server port))
